<template>
  <!-- 筛选区 -->
  <div class="sub-filter">
    <!-- 品牌 -->
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a href="javascript:;" v-for="brand in subCate.brands" @click="selectBrand(brand)"
          :class="{active:brand.selected}" :key="brand.id">{{brand.name}}</a>
      </div>
    </div>
    <!-- 商品属性 -->
    <div class="item" v-for="sale in subCate.saleProperties" :key="sale.id">
      <div class="head">{{sale.name}}：</div>
      <div class="body">
        <a href="javascript:;" v-for="prop in sale.properties" @click="selectAttr(prop, sale)"
          :class="{active:prop.selected}" :key="prop.id">{{prop.name}}</a>
      </div>
    </div>
  </div>
</template>
<script>

export default {
  name: 'SubFilter',
  props: { subCate: { type: Object, default: () => { } } },
  setup (props, { emit }) {
    const getFilter = () => {
      // 收集目前的参数
      const params = {
        brandId: null,
        attrs: []
      }
      // 更新brandId
      const brand = props.subCate.brands.find(it => it.selected)
      params.brandId = brand ? brand.id : null
      // 更新attrs
      props.subCate.saleProperties.forEach(item => {
        const groupName = item.name
        const propertyName = item.properties.find(it => it.selected).name
        if (propertyName !== '全部') {
          params.attrs.push({ groupName, propertyName })
        }
      })
      return params
    }

    //  更新品牌数据
    const selectBrand = (brand) => {
      // console.log(brand)
      props.subCate.brands.forEach(it => { it.selected = false })
      brand.selected = true
      //   收集目前的参数，通知父组件重做查询
      emit('filter-change', getFilter())
    }
    // 1. 更新属性数据
    const selectAttr = (prop, sale) => {
      // console.log(prop, sale)
      sale.properties.forEach(it => { it.selected = false })
      prop.selected = true
      //   收集目前的参数，通知父组件重做查询
      emit('filter-change', getFilter())
    }

    return { selectBrand, selectAttr }
  }
}

</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
